local xiwei = fk.CreateSkill{
  name = "hanqing__xiwei",
  tags = { Skill.Quest },
}

Fk:loadTranslationTable{
  ["hanqing__xiwei"] = "袭围",
  [":hanqing__xiwei"] = "使命技，出牌阶段限一次，你可以弃置所有手牌并视为使用一张伤害牌。<br>\
  ⬤　成功：当有角色因此进入濒死状态时，你获得〖刈旗〗；<br>\
  ⬤　失败：每个回合结束时，若你的“披矢”牌大于等于五张，当前回合角色依次视为对你使用这些牌，然后你将这些牌置入弃牌堆。",

  ["#hanqing__xiwei"] = "袭围：你可以弃置所有手牌，视为使用一张伤害牌",

  ["$hanqing__xiwei1"] = "攻其不备，席卷其营。",
  ["$hanqing__xiwei2"] = "我读万卷兵书，自能破敌无数！",
  ["$hanqing__xiwei3"] = "贼将白起已授首，你们谁还敢来送死？！",
  ["$hanqing__xiwei4"] = "纵死阵前，亦要斗尽最后一策！",
}

xiwei:addEffect("viewas", {
  anim_type = "offensive",
  audio_index = {1, 2},
  prompt = "#hanqing__xiwei",
  interaction = function(self, player)
    local all_names = table.filter(Fk:getAllCardNames("bt"), function (name)
      return Fk:cloneCard(name).is_damage_card
    end)
    local names = player:getViewAsCardNames(xiwei.name, all_names)
    if #names > 0 then
      return UI.CardNameBox {choices = names, all_choices = all_names}
    end
  end,
  card_filter = Util.FalseFunc,
  view_as = function(self, player, cards)
    if not self.interaction.data then return end
    local card = Fk:cloneCard(self.interaction.data)
    card.skillName = xiwei.name
    return card
  end,
  before_use = function(self, player, use)
    use.extraUse = true
    player:throwAllCards("h")
  end,
  enabled_at_play = function(self, player)
    return player:usedEffectTimes(self.name, Player.HistoryPhase) == 0 and
      table.find(player:getCardIds("h"), function(id)
        return not player:prohibitDiscard(id)
      end)
  end,
})

xiwei:addEffect("targetmod", {
  bypass_times = function(self, player, skill, scope, card)
    return card and table.contains(card.skillNames, xiwei.name)
  end,
})

xiwei:addEffect(fk.TurnEnd, {
  anim_type = "negative",
  audio_index = 4,
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(xiwei.name) and not target.dead and #player:getPile("hanqing__pishi") > 4
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    local room = player.room
    room:updateQuestSkillState(player, xiwei.name, true)
    room:invalidateSkill(player, xiwei.name)
    local names = table.map(player:getPile("hanqing__pishi"), function (id)
      return Fk:getCardById(id).name
    end)
    for _, name in ipairs(names) do
      if player.dead or target.dead then break end
      room:delay(800)
      room:useVirtualCard(name, nil, target, player, self.name, true)
    end
    local cards = player:getPile("hanqing__pishi")
    if #cards > 0 then
      room:moveCardTo(cards, Card.DiscardPile, nil, fk.ReasonPutIntoDiscardPile, xiwei.name, nil, true, player)
    end
  end,
})

xiwei:addEffect(fk.EnterDying, {
  anim_type = "special",
  audio_index = 3,
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(xiwei.name) and data.damage and
      data.damage.card and table.contains(data.damage.card.skillNames, xiwei.name)
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    local room = player.room
    room:updateQuestSkillState(player, xiwei.name, false)
    room:handleAddLoseSkills(player, "hanqing__yiqi")
    room:invalidateSkill(player, xiwei.name)
  end,
})

xiwei:addAI(nil, "vs_skill")

return xiwei
